package com.mep.auth.controller;

import com.mep.auth.constant.SysConstants;
import com.mep.auth.dao.SysRoleMapper;
import com.mep.auth.feign.IFeignSysRoleApi;
import com.mep.auth.model.SysRole;
import com.mep.auth.model.SysRoleMenu;
import com.mep.auth.service.SysRoleService;
import com.mep.common.http.HttpResult;
import com.mep.common.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色控制器
 * @author lyl
 * @date Jan 13, 2019
 */
@Api(value="002-role的controller",tags="002-role的controller")
@RestController
@RequestMapping("/role")
public class SysRoleController implements IFeignSysRoleApi {

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Override
	@ApiOperation(value="01.保存角色信息",notes="问题点:保存角色信息")
	@ApiOperationSupport(order = 1, author = "lyl")
//	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysRole record) {
		SysRole role = sysRoleService.findById(record.getId());
		if(role != null) {
			if(SysConstants.ADMIN.equalsIgnoreCase(role.getName())) {
				return HttpResult.error("超级管理员不允许修改!");
			}
		}
		// 新增角色
		if((record.getId() == null || record.getId() ==0) && !sysRoleService.findByName(record.getName()).isEmpty()) {
			return HttpResult.error("角色名已存在!");
		}
		return HttpResult.ok(sysRoleService.save(record));
	}

	@Override
	@ApiOperation(value="02.删除角色信息",notes="问题点:删除角色信息")
	@ApiOperationSupport(order = 2, author = "lyl")
//	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysRole> records) {
		return HttpResult.ok(sysRoleService.delete(records));
	}

	@Override
	@ApiOperation(value="03.查询角色的分页信息",notes="问题点:查询角色的分页信息")
	@ApiOperationSupport(order = 3, author = "lyl")
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(sysRoleService.findPage(pageRequest));
	}

	@Override
	@ApiOperation(value="04.查询角色list信息",notes="问题点:查询角色list信息")
	@ApiOperationSupport(order = 4, author = "lyl")
//	@PostMapping(value="/findAll")
	public HttpResult findAll() {
		return HttpResult.ok(sysRoleService.findAll());
	}

	@Override
	@ApiOperation(value="05.通过角色Id查询角色菜单信息",notes="问题点:通过角色Id查询角色菜单信息")
	@ApiOperationSupport(order = 5, author = "lyl")
//	@PostMapping(value="/findRoleMenus")
	public HttpResult findRoleMenus(@RequestParam Long roleId) {
		return HttpResult.ok(sysRoleService.findRoleMenus(roleId));
	}

	@Override
	@ApiOperation(value="06.保存角色权限信息",notes="问题点:保存角色权限信息")
	@ApiOperationSupport(order = 6, author = "lyl")
//	@PostMapping(value="/saveRoleMenus")
	public HttpResult saveRoleMenus(@RequestBody List<SysRoleMenu> records) {
		for(SysRoleMenu record:records) {
			SysRole sysRole = sysRoleMapper.selectByPrimaryKey(record.getRoleId());
			if(SysConstants.ADMIN.equalsIgnoreCase(sysRole.getName())) {
				// 如果是超级管理员，不允许修改
				return HttpResult.error("超级管理员拥有所有菜单权限，不允许修改！");
			}
		}
		return HttpResult.ok(sysRoleService.saveRoleMenus(records));
	}
}
